package com.fneuch.mwg.army;

import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.fneuch.mwg.profiles.ModelProfile;


public class Unit {

	@NotNull
	@Size(min=1, max=25)
	private String name;
	
	private Map<ModelProfile, Integer> models = new HashMap<ModelProfile, Integer>();
			
	public Unit(String name) {
		super();
		setName(name);
	}
	
	public String getName() {
		return name;
	}
	
	private void setName(String name) {
		this.name = name;
	}

	public Map<ModelProfile, Integer> getModels() {
		return models;
	}

	public void setModels(Map<ModelProfile, Integer> models) {
		this.models = models;
	}
	
	public void ajouterModel(int nbModel, ModelProfile model) {
		int total = nbModel;
		if(models.containsKey(model)) {
			total += models.get(model).intValue();
		}
		models.put(model, Integer.valueOf(total));
	}

	public boolean removeModelbyName(String modelName) {
		boolean success = false;
		for(ModelProfile model: models.keySet()) {
			if(model.getName().equals(modelName)){
				success = true;
				models.remove(model);
			}
		}

		return success;
	}

	public int getNbModels() {
		int nbModel = 0;
		for(ModelProfile model: models.keySet()) {
			nbModel += models.get(model).intValue();
		}
		return nbModel;
	}

	public ModelProfile getLargestNumberModel(Unit defender) {
		ModelProfile largestOne = null;
		int largestNbModel = 0;

		for(ModelProfile model: models.keySet()) {
			int nbModel =models.get(model).intValue(); 
			if(nbModel>largestNbModel) {
				largestOne = model;
				largestNbModel = nbModel;
			}
		}
		
		return largestOne;
	}
	
}
